/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TabelaRegistros.java
 *
 * Created on May 3, 2009, 4:07:56 AM
 */
package dialogs;

import util.CategoriaNode;
import java.util.List;
import java.util.Vector;
import javax.swing.JTabbedPane;
import mobcatadmin.MobCatAdminView;
import persistencia.controller.jpa.CategoriaJpaController;
import persistencia.entidade.Categoria;
import persistencia.entidade.Registro;
import util.ListaRegistroCellRenderer;

/**
 *
 * @author Novo usuário
 */
public class TabelaRegistros extends javax.swing.JPanel {

    private Categoria categoria = null;
    private MobCatAdminView parent = null;
    private List<Registro> reglist;
    private MobCatTree tree;
    private CategoriaJpaController catJpa = new CategoriaJpaController();

    /** Creates new form TabelaRegistros */
    public TabelaRegistros(Categoria cat, MobCatAdminView parent, MobCatTree tree) {
        this.parent = parent;
        this.categoria = catJpa.findCategoria(cat.getCategoriaId());
        this.tree = tree;
        reglist = cat.getRegistroCollection();
        initComponents();
        loadTable();
    }

    public TabelaRegistros(List<Registro> regList, MobCatAdminView parent) {
        this.parent = parent;
        this.reglist = regList;
        initComponents();
        loadTable();
    }

    public void loadTable() {

        // JPanel p = new JPanel();

        //BorderLayout b = new BorderLayout();
        //for (int i = 0; i < reglist.size(); i++) {
        //  Registro registro = reglist.get(i);
        //jScrollPane1.add(new RegistroLista(registro));
        //}
    }

    public Vector<RegistroLista> loadList() {
        if (categoria != null) {
            this.categoria = catJpa.findCategoria(categoria.getCategoriaId());
            reglist = categoria.getRegistroCollection();
        }
        Vector<RegistroLista> regsTela = new Vector<RegistroLista>();
        for (int i = 0; i < reglist.size(); i++) {
            Registro registro = reglist.get(i);
            regsTela.add(new RegistroLista(registro));
        }
        return regsTela;
    }

    public Categoria getCategoria() {
        return categoria;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnClose = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstRegistros = new javax.swing.JList(loadList());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mobcatadmin.MobCatAdminApp.class).getContext().getResourceMap(TabelaRegistros.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setMaximumSize(new java.awt.Dimension(32767, 100));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(779, 100));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(mobcatadmin.MobCatAdminApp.class).getContext().getActionMap(TabelaRegistros.class, this);
        btnClose.setAction(actionMap.get("fechatAction")); // NOI18N
        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(623, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(btnClose))
                .addContainerGap())
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setPreferredSize(new java.awt.Dimension(258, 230));

        lstRegistros.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstRegistros.setCellRenderer(new ListaRegistroCellRenderer());
        lstRegistros.setName("lstRegistros"); // NOI18N
        lstRegistros.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstRegistrosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstRegistros);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 843, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 843, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        JTabbedPane parent = (JTabbedPane) this.getParent();
        parent.remove(this);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        Registro reg = new Registro();
        List<Categoria> cats = reg.getCategoriaCollection();
        cats.add(((CategoriaNode) (javax.swing.tree.DefaultMutableTreeNode) tree.getTree().getSelectionPath().getLastPathComponent()).getCategoria());
        parent.getTabRegistro().add("Novo Registro", new TelaRegistro(reg, this, tree));


    }//GEN-LAST:event_jButton1ActionPerformed

    private void lstRegistrosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstRegistrosValueChanged

        RegistroLista reg = (RegistroLista) lstRegistros.getSelectedValue();
        TelaRegistro tr = new TelaRegistro(reg.getRegistro(), this, tree);
        parent.getTabRegistro().add(reg.getRegistro().getNome(), tr);
        parent.getTabRegistro().setSelectedComponent(tr);


    }//GEN-LAST:event_lstRegistrosValueChanged

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TabelaRegistros) {
            if (((TabelaRegistros) obj).getCategoria() != null && categoria != null) {
                return ((TabelaRegistros) obj).getCategoria().getCategoriaId().intValue() == categoria.getCategoriaId().intValue() ? true : false;
            } else {
                return true;
            }
        }
        return false;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstRegistros;
    // End of variables declaration//GEN-END:variables
}
